
// var api_host = 'https://api1.maxdoge.com';//测试环境
// var api_host = 'https://api.maxdoge.com';
var api_host = 'https://admin.jawanology.com';
var publiccode = 'ZGZvI1mi8Q';
var upyun_imgHost = "https://photo.yixunwx.top";
var upyun_bucket = "yixunweixing";
var upyun_opterator = "yixunweixing";//又拍云空间操作员账户
var upyun_getSignUrl = api_host + "/api.php?ac=signupyun&publickey=" + publiccode;

var appType = 1;//1、带销售功能小程序  2、不带销售功能小程序 3、服务类型企业小程序


var version = "v1.0.0";
var appid = "wx5c4bb1c7254e0aa6";
var orgid = "211661";//濠飲濠食 正式环境

function formatTime(date, day_join, time_join) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()

  if (time_join) {
    return [year, month, day].map(formatNumber).join(day_join) + ' ' + [hour, minute, second].map(formatNumber).join(time_join)
  } else {
    return [year, month, day].map(formatNumber).join(day_join)
  }

}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function api(d){
  d.data.binduid = this.orgid;
  if(!d.data.publickey){
    d.data.publickey = this.publiccode;
  }
  if(!d.method) d.method="GET";
  console.log("请求参数：", d.data);
  var header = d.header ? d.header :{
      "Content-Type": "application/json" 
  };
  if (d.method == "POST" || d.method == "post") {
    d.data = json2Form(d.data);
    header = {
      "Content-Type": "application/x-www-form-urlencoded" 
    };
  }
  wx.request({
    url: api_host + "/api.php",
    method:d.method,
    data:d.data,
    success: function(responseData){
      if(responseData.statusCode == 200){
        d.success(responseData);
      }
    },
    header: header,  
    fail: function(e) {
      console.log(e);
    }
  });
}

function json2Form(json) {  
    var str = [];  
    for(var p in json){  
        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));  
    }  
    return str.join("&");  
}

module.exports = {
  formatTime: formatTime,
  formatNumber: formatNumber,
  api:api,
  json2Form:json2Form,
  publiccode:publiccode,
  api_host: api_host,
  upyun_bucket: upyun_bucket,
  upyun_imgHost: upyun_imgHost,
  upyun_opterator: upyun_opterator,
  // upyun_apikey: upyun_apikey,
  upyun_getSignUrl: upyun_getSignUrl,

  appid: appid,
  orgid: orgid,
  version: version,
  appType: appType,

}
